﻿using UnitOfWorkDemo.Infrastructure;

namespace UnitOfWorkDemo.Model
{
    // 账号转账领域服务类
    public class AccountService
    {
        private readonly IAccountRepository _productRepository;
        private readonly IUnitOfWork _unitOfWork;

        public AccountService(IAccountRepository productRepository, IUnitOfWork unitOfWork)
        {
            _productRepository = productRepository;
            _unitOfWork = unitOfWork;            
        }
        
        public void Transfer(Account from, Account to, decimal amount)
        {
            if (from.Balance >= amount)
            {
                from.Balance -= amount;
                to.Balance += amount;

                _productRepository.Save(from);
                _productRepository.Save(to);
                _unitOfWork.Commit();
            }
        } 
    }
}